var express = require("express"),
  path = require("path");
// const router = exprss.Router()
// var cors = require('cors')
// app.use(cors())
var app = express();
// const http = require('http');
// const server = http.createServer(app);

app.all("*", function (req, res, next) {
  res.header("access-control-allow-origin", "*"); //当允许携带cookies此处的白名单不能写’’
  res.header(
    "Access-Control-Allow-Headers",
    "content-type,Content-Length, Authorization,Origin,Accept,X-Requested-With"
  ); //允许的请求头
  res.header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT"); //允许的请求方法
  res.header("Access-Control-Allow-Credentials", true); //允许携带cookies
  next();
});
var bodyParser = require("body-parser");
app.use(express.static(path.join(__dirname, "assets")));
app.use(express.json({ limit: "50mb" })); //解决413Payload Too Large
app.use(express.urlencoded({ limit: "50mb", extended: true })); //解决413Payload Too Large
// app.use(express.bodyParser({limit : "50mb"}));
const fs = require("fs");

// 创建application/json 解析器
var jsonParser = bodyParser.json();

// 创建 application/x-www-form-urlencoded 解析器
var urlencodedParser = bodyParser.urlencoded({ extended: false });

const userCRUD = require("./UserModule/userCRUD.js");
app.use("/userCRUD", userCRUD);

const articleCRUD = require("./articleModel/articleCRUD.js");
app.use("/articleCRUD", articleCRUD);

const socketCRUD = require("./SocketModel/socketCRUD.js");
app.use("/socketCRUD", socketCRUD);

const FriendLinkCRUD = require("./FriendLinkModule/FriendLinkCRUD.js");
app.use("/FriendLinkCRUD", FriendLinkCRUD);

const MusicCRUD = require("./MusicModule/MusicCRUD.js");
app.use("/MusicCRUD", MusicCRUD);

const resourcesCRUD = require("./resourcesModel/resourcesCRUD.js");
app.use("/resourcesCRUD", resourcesCRUD);

// 侧边数据
app.get("/side_list", function (req, res) {
  fs.readFile("./data/side_list.json", "utf-8", (err, data) => {
    if (err) return;
    res.send(JSON.parse(data));
  });
});

// 上传图片
const img = require("./upload.js");
// const { send } = require("process");
app.use("/up", img);

// 对页面 abcd, abxcd, ab123cd, 等响应 GET 请求
app.get("/ab*cd", function (req, res) {
  console.log("/ab*cd GET 请求");
  res.send("正则匹配");
});

/* 获取小程序AeeassToken */
app.get("/getWxAeeassToken", function (req, res) {
  res.redirect(
    `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${"wxcc302680ba27e304"}&secret=98df50759418bc4b926107c2b569fa0e`
  );
});

/* 新增集合，数据库表 */
app.post("/addDataBase", function (req, res) {
  const { accessToken } = req.query;
  console.log("access_token", accessToken);
  // location
  res.redirect(
    307,
    `https://api.weixin.qq.com/tcb/databasecollectionadd?access_token=${accessToken}`
  );
  res.send();
});

/* 下载文件 */
const downloadModule = require("./downloadModule/downloadModule.js");
app.use("/downloadModule", downloadModule);

const server = app.listen(5006, function () {
  // var host = server.address().address
  // var port = server.address().port
  console.log("BLOG实例，访问地址为 47.107.243.60:5006"); //47.107.243.60为服务器ip,实为localhost
});

require("./mysocket.js")(server);
